package com.example.dy.dy_bigtask.action;

import com.example.dy.dy_bigtask.dao.UserMapper;
import com.example.dy.dy_bigtask.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.Enumeration;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserAction {

    @Autowired
    UserMapper userMapper;
//18211170204丁垚
    @GetMapping("/password")
    public String renew(){
        return "pwd";
    }



    @RequestMapping("/login")
    public String login1(User user, HttpSession session){
        User user1 = userMapper.findUserByNameAndPWD(user);
        if (user1 != null){
            session.setAttribute("userinfo", user1);
            System.out.println(user1.getUserName());
            return "redirect:/sharper/listsharpers";
        }
        return "redirect:/login";

    }



    @RequestMapping(value = "/sessionOut")
    public String sessionOut(HttpServletRequest request){
        // session.invalidate();
        // request.getSession().invalidate();
        // 清除session
        Enumeration<String> attributeNames = request.getSession().getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String key = attributeNames.nextElement().toString();
            request.getSession().removeAttribute(key);
        }
        return "welcome";
    }



    @RequestMapping(value = "/getUsers")
    @ResponseBody
    public List<User> getUsers(){
        List<User> users = userMapper.selectAll();
        return users;
    }
}
